<?php

use Fisharebest\Webtrees\GedcomTag;
use Fisharebest\Webtrees\Http\RequestHandlers\SearchAdvancedAction;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Tree;

/**
 * @var array<string,string> $date_options
 * @var array<string,string> $fields
 * @var array<string,string> $field_labels
 * @var array<string,string> $name_options
 * @var array<string,string> $other_options
 * @var string               $title
 * @var Tree                 $tree
 */

?>

<h2 class="wt-page-title">
    <?= $title ?>
</h2>

<div id="advanced-search-page">
    <form method="post" action="<?= e(route(SearchAdvancedAction::class, ['tree' => $tree->name()])) ?>" class="wt-page-options wt-page-options-search-advanced hidden-print mb-4">
        <?= csrf_field() ?>

        <div class="row form-group wt-page-options-label mt-4">
            <div class="form-row mx-auto">
                <?= I18N::translate('Individual') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (substr($field_name, 0, 5) !== 'FAMC:') : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name] ?? GedcomTag::getLabel($field_name), 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <div class="row form-group">
            <div class="col-sm-3 col-form-label wt-page-options-label">
                <?= I18N::translate('Add more fields') ?>
            </div>

            <div class="col-sm-3 wt-page-options-value">
                <?= view('components/select', ['name' => 'other_field', 'selected' => '', 'options' => ['' => ''] + $other_fields]) ?>
            </div>

            <div class="col-sm-3 wt-page-options-value">
                <input type="text" class="form-control" name="other_value">
            </div>

            <div class="col-sm-3 wt-page-options-value">
            </div>
        </div>

        <div class="row form-group wt-page-options-label mt-4">
            <div class="form-row mx-auto">
                <?= I18N::translate('Father') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (substr($field_name, 0, 10) === 'FAMC:HUSB:') : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name], 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <div class="row form-group wt-page-options-label mt-4">
            <div class="form-row mx-auto">
                <?= I18N::translate('Mother') ?>
            </div>
        </div>

        <?php foreach ($fields as $field_name => $field_value) : ?>
            <?php if (substr($field_name, 0, 10) === 'FAMC:WIFE:') : ?>
                <?= view('search-advanced-field', ['field_label' => $field_labels[$field_name], 'field_name' => $field_name, 'field_value' => $field_value, 'modifier' => $modifiers[$field_name] ?? '', 'date_options' => $date_options, 'name_options' => $name_options]) ?>
            <?php endif ?>
        <?php endforeach ?>

        <div class="row form-group my-3">
            <div class="form-row mx-auto">
                <input type="submit" class="btn btn-primary" value="<?= /* I18N: A button label. */
                I18N::translate('search') ?>">
            </div>
        </div>
    </form>
</div>

<?php if (true) : ?>
    <?php if ($individuals->isEmpty()) : ?>
        <div class="alert alert-info row">
            <?= I18N::translate('No results found.') ?>
        </div>
    <?php else : ?>
        <?= view('search-results', ['individuals' => $individuals, 'search_families' => false, 'search_individuals' => true, 'search_notes' => false, 'search_sources' => false, 'search_repositories' => false, 'tree' => $tree]) ?>
    <?php endif ?>
<?php endif ?>

